package com.ktc.article.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;
import com.ktc.article.dao.ColumnDao;
import com.ktc.article.pojo.Column;

/**
 * @Description 专栏 服务层
 * @author admin
 * @date 2021-04-12 11:35:25
*/
@Service
public class ColumnService {

	@Autowired
	private ColumnDao columnDao;

	@Autowired
	private IdWorker idWorker;

	/**
	* 查询全部列表
	* @return
	*/
	public List<Column> findAll() {
		return columnDao.findAll();
	}

	/**
	* 条件查询+分页
	* @param whereMap
	* @param page
	* @param size
	* @return
	*/
	public Page<Column> findSearch(Map whereMap, int page, int size) {
		Specification<Column> specification = createSpecification(whereMap);
		PageRequest pageRequest =  PageRequest.of(page-1, size);
		return columnDao.findAll(specification, pageRequest);
	}

	/**
 	* 条件查询
	* @param whereMap
	* @return
	*/
	public List<Column> findSearch(Map whereMap) {
		Specification<Column> specification = createSpecification(whereMap);
		return columnDao.findAll(specification);
	}

	/**
	* 根据ID查询实体
	* @param id
	* @return
	*/
	public Column findById(String id) {
		return columnDao.findById(id).get();
	}

	/**
	* 增加
	* @param column
	*/
	public void add(Column column) {
		column.setId( idWorker.nextId()+"" );
		columnDao.save(column);
	}

	/**
	* 修改
	* @param column
	*/
	public void update(Column column) {
		columnDao.save(column);
	}

	/**
	* 删除
	* @param id
	*/
	public void deleteById(String id) {
		columnDao.deleteById(id);
	}

	/**
	* 动态条件构建
	* @param searchMap
	* @return
	*/
	private Specification<Column> createSpecification(Map searchMap) {

		return new Specification<Column>() {

			@Override
			public Predicate toPredicate(Root<Column> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
				//专栏名称
				if (searchMap.get("name")!=null && !"".equals(searchMap.get("name"))) {
					predicateList.add(cb.like(root.get("name").as(String.class), "%"+(String)searchMap.get("name")+"%"));
				}
				//专栏简介
				if (searchMap.get("summary")!=null && !"".equals(searchMap.get("summary"))) {
					predicateList.add(cb.like(root.get("summary").as(String.class), "%"+(String)searchMap.get("summary")+"%"));
				}
				//用户ID
				if (searchMap.get("userid")!=null && !"".equals(searchMap.get("userid"))) {
					predicateList.add(cb.like(root.get("userid").as(String.class), "%"+(String)searchMap.get("userid")+"%"));
				}
				//申请日期
				if (searchMap.get("createtime")!=null && !"".equals(searchMap.get("createtime"))) {
					predicateList.add(cb.like(root.get("createtime").as(String.class), "%"+(String)searchMap.get("createtime")+"%"));
				}
				//审核日期
				if (searchMap.get("checktime")!=null && !"".equals(searchMap.get("checktime"))) {
					predicateList.add(cb.like(root.get("checktime").as(String.class), "%"+(String)searchMap.get("checktime")+"%"));
				}
				//状态
				if (searchMap.get("state")!=null && !"".equals(searchMap.get("state"))) {
					predicateList.add(cb.like(root.get("state").as(String.class), "%"+(String)searchMap.get("state")+"%"));
				}

				return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));
			}
		};
	}

}
